<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="baidu-site-verification" content="DKi2gJy3XK"/>


        <link rel="shortcut icon" href="/img/favicon.ico">

    <!--Description-->
    
        <meta name="description" content="ZZZXXXCCCWXY999的个人博客，佛系写博客">
    

    <!--Author-->
    
        <meta name="author" content="ZZZXXXCCCWXY999">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="LeetCode 57-II 和为S的连续正整数序列题解"/>
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="ZZZXXXCCCWXY999的个人博客，佛系写博客"/>
    

    <!--Open Graph Site Name-->
    <meta property="og:site_name" content="ZZZXXXCCCWXY999的个人博客"/>

    <!--Type page-->
    
        <meta property="og:type" content="article"/>
    

    <!--Page Cover-->
    

    <meta name="twitter:card" content="summary"/>
    

    <!-- Title -->
    
    <title>LeetCode 57-II 和为S的连续正整数序列题解 - ZZZXXXCCCWXY999的个人博客</title>

    <!-- Tachyons Core CSS -->
    <link rel="stylesheet" href="/css/tachyons.min.css">
<!--    <link rel="stylesheet" href="https://unpkg.com/tachyons/css/tachyons.min.css">-->

    <!-- Custom Fonts -->
<!--    <link href="/css/font-awesome.min.css" rel="stylesheet" type="text/css">-->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>-->
<!--    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>-->
    <script src="/js/html5shiv.js"></script>
<!--    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>-->
    <script src="/js/respond.min.js"></script>
    <![endif]-->

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/css/style.css">


    <!-- Google Analytics -->
    


<meta name="generator" content="Hexo 4.2.0"></head>


<body>

<!-- Main Content -->
<!-- Banner -->
<!-- Banner -->
<div class="w-100 bg-1 ph5-ns ph3 text-light">
    
    <nav class="db dt-l w-100 mw8 center border-box pv3">
        <a class="db dtc-l v-mid link dim w-100 w-25-l tc tl-l mb2 mb0-l white" href="/" title="ZZZXXXCCCWXY999的个人博客">
            <img src="http://www.codeblocq.com/assets/projects/hexo-theme-anodyne/assets/anodyne.svg" class="dib h3" alt="ZZZXXXCCCWXY999的个人博客">
        </a>
        <div class="db dtc-l v-mid w-100 w-75-l tc tr-l">
            
                <a class="link dim f6 f5-l dib mr3 mr4-l white" 
                    href="/" 
                    title="首页">
                    首页
                </a>
            
                <a class="link dim f6 f5-l dib mr3 mr4-l white" 
                    href="/archives" 
                    title="博客列表">
                    博客列表
                </a>
            
                <a class="link dim f6 f5-l dib mr3 mr4-l white" 
                    href="/tags" 
                    title="标签">
                    标签
                </a>
            
                <a class="link dim f6 f5-l dib mr3 mr4-l white" 
                    href="/categories" 
                    title="分类">
                    分类
                </a>
            
                <a class="link dim f6 f5-l dib mr3 mr4-l white" 
                    href="/about" 
                    title="关于">
                    关于
                </a>
            
                <a class="link dim f6 f5-l dib mr3 mr4-l white" 
                    href="/contact" 
                    title="联系">
                    联系
                </a>
            
        </div>
    </nav>

    <!-- Title -->
    <div class="w-100 mw8 center vh-40 dt">
        <div class="dtc v-mid white">
            <h1 class="f1-l f2-m tc tc-m tl-ns">LeetCode 57-II 和为S的连续正整数序列题解</h1>
            <p class="f4 fw3 pab-100px tc tc-m tl-ns">2020-03-06</p>
        </div>
    </div>

    <!-- Icon -->
    <div class="relative w-100 mw8 center white dn dn-m db-ns">
        <i class="header-icon fa fa-file-text-o"></i>
    </div>
</div>

<!-- Content -->
<div class="w-100 ph2 ph4-m ph5-l mv5 mv6-l">
    <div class="content">
        <div class="mw8 center">
            <div class="cf">
                <div class="fl w-100 w-70-l mw7 left fw3 lh-copy pr4-ns pr0-m post-content">
                    <!-- Tags Vertical -->
                    
                        <div class="tags-container-vertical">
                            <div class="tags-sub-container">
                                <a class="fw3 ph1 dib" href="/tags/java/">#java</a>
                            </div>
                        </div>
                    

                    <!-- Main Post Content -->
                    <p align="right"> ---ZZZXXXCCCWXY999</p>

<h4 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h4><p>输入一个正整数 target ，输出所有和为 target 的连续正整数序列（至少含有两个数）。</p>
<p>序列内的数字由小到大排列，不同序列按照首个数字从小到大排列。</p>
<h4 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h4><p>示例 1：</p>
<p>输入：target = 9<br>输出：[[2,3,4],[4,5]]<br>示例 2：</p>
<p>输入：target = 15<br>输出：[[1,2,3,4,5],[4,5,6],[7,8]]</p>
<h4 id="限制"><a href="#限制" class="headerlink" title="限制"></a>限制</h4><p>1 &lt;= target &lt;= 10^5</p>
<h4 id="解题思路"><a href="#解题思路" class="headerlink" title="解题思路"></a>解题思路</h4><h5 id="方法一-暴力："><a href="#方法一-暴力：" class="headerlink" title="方法一 暴力："></a>方法一 暴力：</h5><p>每个正整数为起点，找到以该正整数为起点的序列和等于target，如果序列和小雨target则继续往右加，如果序列和大于target则找下一个正整数为起点。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 统计目录中所有java文件的行数的总和，注释行数总和，空行数总和，文件数总和</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> capacity = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span>[][] ans = <span class="keyword">new</span> <span class="keyword">int</span>[capacity][];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[][] findContinuousSequence(<span class="keyword">int</span> target) &#123;</span><br><span class="line">        <span class="keyword">int</span> p = <span class="number">0</span>;<span class="comment">//用来让ans[]递增</span></span><br><span class="line">        <span class="keyword">int</span> sum = <span class="number">0</span>, limit = (target - <span class="number">1</span>) / <span class="number">2</span>; <span class="comment">// (target - 1) / 2 等效于 target / 2 下取整</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= limit; ++i) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = i; ; ++j) &#123;</span><br><span class="line">                sum += j;</span><br><span class="line">                <span class="keyword">if</span> (sum &gt; target) &#123;</span><br><span class="line">                    sum = <span class="number">0</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (sum == target) &#123;</span><br><span class="line">                    <span class="keyword">int</span> q = <span class="number">0</span>;<span class="comment">//用来让res[]递增</span></span><br><span class="line">                    <span class="keyword">int</span>[] res = <span class="keyword">new</span> <span class="keyword">int</span>[j - i + <span class="number">1</span>];</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> k = i; k &lt;= j; ++k) &#123;</span><br><span class="line">                        res[q++] = k;</span><br><span class="line">                    &#125;</span><br><span class="line">                    ans[p++] = res;</span><br><span class="line">                    <span class="comment">//如果ans[]的空间满了，我们需要给它扩容空间，哈皮题目要求的返回int[][]我们也没办法。。。</span></span><br><span class="line">                    <span class="keyword">if</span> (p == capacity) &#123;</span><br><span class="line">                        enlargeCapacity();</span><br><span class="line">                    &#125;</span><br><span class="line">                    sum = <span class="number">0</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        changeCapacity(p);</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 扩容空间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">enlargeCapacity</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        capacity += <span class="number">10</span>;</span><br><span class="line">        ans = Arrays.copyOf(ans, capacity);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 删除数组多余空间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">changeCapacity</span><span class="params">(<span class="keyword">int</span> retCapacity)</span> </span>&#123;</span><br><span class="line">        ans = Arrays.copyOf(ans, retCapacity);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>#####方法二 双指针：</p>
<p>我们发现上面的方法非常麻烦时间复杂度和很高，而且求和也非常的笨。。。因为我们有数学公式可以用来求和sum = (l + r) * (r - l + 1) / 2 ，l和r分别表示序列两端的数。这样我们只需要知道l和r就可以算出来他的序列。我们对方法一进行一些优化：</p>
<p>1.不需要每次都重新求和一遍，只需要知道l和r就能求和，如果sum&lt;target我们就让r++，如果sum&gt;target我们就让l++，如果sum==target我们把他加入到数组中。</p>
<p>2.由于题目要求每个序列至少含有两个数，所以我们只需要保证l&lt;r就可以。</p>
<p>3.上代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> xyz.zxcwxy999.LeetCode.interview57II;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> capacity = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">int</span>[][] ans = <span class="keyword">new</span> <span class="keyword">int</span>[capacity][];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[][] findContinuousSequence(<span class="keyword">int</span> target) &#123;</span><br><span class="line">        <span class="keyword">int</span> l = <span class="number">1</span>; <span class="comment">//让l初始化为1，r初始化为2</span></span><br><span class="line">        <span class="keyword">int</span> r = <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> p = <span class="number">0</span>; <span class="comment">//用来让ans[]递增</span></span><br><span class="line">        <span class="keyword">int</span> sum = (l + r) * (r - l + <span class="number">1</span>) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">while</span> (l &lt; r) &#123;</span><br><span class="line">            <span class="keyword">if</span> ((l + r) * (r - l + <span class="number">1</span>) / <span class="number">2</span> &lt; target) &#123;</span><br><span class="line">                r++;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> ((l + r) * (r - l + <span class="number">1</span>) / <span class="number">2</span> &gt; target) &#123;</span><br><span class="line">                l++;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">int</span> q = <span class="number">0</span>;<span class="comment">//用来让res[]递增</span></span><br><span class="line">                <span class="keyword">int</span>[] res = <span class="keyword">new</span> <span class="keyword">int</span>[r - l + <span class="number">1</span>];</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = l; i &lt;= r; i++) &#123;</span><br><span class="line">                    res[q++] = i;</span><br><span class="line">                &#125;</span><br><span class="line">                ans[p++] = res;</span><br><span class="line">                <span class="keyword">if</span> (p == capacity) &#123;</span><br><span class="line">                    enlargeCapacity();</span><br><span class="line">                &#125;</span><br><span class="line">                l++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        changeCapacity(p);</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 扩容空间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">enlargeCapacity</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        capacity += <span class="number">10</span>;</span><br><span class="line">        ans = Arrays.copyOf(ans, capacity);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 删除数组多余空间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">changeCapacity</span><span class="params">(<span class="keyword">int</span> retCapacity)</span> </span>&#123;</span><br><span class="line">        ans = Arrays.copyOf(ans, retCapacity);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>#####方法三 数学方法：</p>
<p>首先，我们没必要从1一直判断到target-1。我们只需要判断到target/2即可</p>
<p>其次，我们有了求和公式：(l + r) * (r - l + 1) / 2，我们可以令它等于target</p>
<p>(l + r) * (r - l + 1) / 2=targe</p>
<p>r^2+r-l^2+l-2*target=0</p>
<p>有点难看。。。。。。。变成x和y吧，，，，，y^2+y-x^2+x-2*target=0</p>
<p>解关于y的二元一次方程</p>
<p>a=1,b=1,c=-x^2+x-2*target</p>
<p>结合题目情况考虑，x和y的解为整数，判别式为整数，求根公式也为整数（2a分之-b加减根号下b^2-4ac）因为a=1，所以我们只需要要求-b加减根号下b^2-4ac为偶数。。。</p>
<p>上代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.lang.Math.sqrt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> capacity = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span>[][] ans = <span class="keyword">new</span> <span class="keyword">int</span>[capacity][];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[][] findContinuousSequence(<span class="keyword">int</span> target) &#123;</span><br><span class="line">        <span class="keyword">int</span> p = <span class="number">0</span>; <span class="comment">//用来让ans[]递增</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">1</span>; x &lt;= (target - <span class="number">1</span>) / <span class="number">2</span>; ++x) &#123;</span><br><span class="line">            <span class="keyword">long</span> delta = <span class="number">1</span> - <span class="number">4</span> * (-(<span class="keyword">long</span>) x * (<span class="keyword">long</span>) x +x - <span class="number">2</span> * target); <span class="comment">//判别式b^2-4ac</span></span><br><span class="line">            <span class="keyword">if</span> (delta &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">int</span> delta_sqrt = (<span class="keyword">int</span>) sqrt(delta + <span class="number">0.5</span>); <span class="comment">//根号下b^2-4ac，加0.5是为了四舍五入</span></span><br><span class="line">                <span class="keyword">if</span> ((<span class="keyword">long</span>) delta_sqrt * (<span class="keyword">long</span>) delta_sqrt == delta &amp;&amp; (delta_sqrt - <span class="number">1</span>) % <span class="number">2</span> == <span class="number">0</span>) &#123;<span class="comment">//前者判断是否为整数，后者判断是否分子为偶数</span></span><br><span class="line">                    <span class="keyword">int</span> y = (-<span class="number">1</span> + delta_sqrt) / <span class="number">2</span>; <span class="comment">// 另一个解(-1-delta_sqrt)/2必然小于0，不用考虑</span></span><br><span class="line">                    <span class="keyword">if</span> (x &lt; y) &#123;</span><br><span class="line">                        <span class="keyword">int</span> q = <span class="number">0</span>;<span class="comment">//用来让res[]递增</span></span><br><span class="line">                        <span class="keyword">int</span>[] res = <span class="keyword">new</span> <span class="keyword">int</span>[y - x + <span class="number">1</span>];</span><br><span class="line">                        <span class="keyword">for</span> (<span class="keyword">int</span> i = x; i &lt;= y; i++) &#123;</span><br><span class="line">                            res[q++] = i;</span><br><span class="line">                        &#125;</span><br><span class="line">                        ans[p++] = res;</span><br><span class="line">                        <span class="keyword">if</span> (p == capacity) &#123;</span><br><span class="line">                            enlargeCapacity();</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        changeCapacity(p);</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 扩容空间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">enlargeCapacity</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        capacity += <span class="number">10</span>;</span><br><span class="line">        ans = Arrays.copyOf(ans, capacity);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 删除数组多余空间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">changeCapacity</span><span class="params">(<span class="keyword">int</span> retCapacity)</span> </span>&#123;</span><br><span class="line">        ans = Arrays.copyOf(ans, retCapacity);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
                    
                    <!-- Tags Bottom -->
                    
                        <div class="tags-container-bottom">
                            <i class="fa fa-tag pr3 text-main-color"></i><a class="fw3 ph1 dib" href="/tags/java/">#java</a>
                        </div>
                    

                    <!-- Comments -->
                    



                </div>
                <div class="fl w-100 w-30-l center fw3 lh-copy pl4-ns tl black-50">
                    
                    <hr class="dn-l mw4 black-50 mt5" />
                    
                    <!-- Widget 1: About -->
                    <div class="mt5 mt0-l">
    <article class="dt db-l mw8 mw8-m mw5-ns center ml0-l bg-white mv3">
        <div class="dn dtc-m db-l v-mid tc pr4 pr0-l" style="min-width: 6rem;">
            <img src="https://avatars0.githubusercontent.com/u/47167745?s=460&v=4" class="mb4-l br-100 h3 w3 h4-l w4-l dib" title="ZZZXXXCCCWXY999">
        </div>
        <div class="dtc db-l v-mid lh-copy measure center f6 black-50 tj">
            我是ZZZXXXCCCWXY999，这是我的博客。<br>没错，看我的ID就能知道我是一个巨星。<br>我会在博客里写一些我在专业方面的分享。
        </div>
    </article>
</div>

                    <hr class="dn-l mw4 black-50 mt5" />
                    
                    <!-- Widget 2: Categories -->
                    
                        <div class="mt5 tc tl-l">
    <h3>分类</h3>
    
        <p>
            <a href="/categories/算法/">算法</a>
        </p>
    
</div>


                        <hr class="dn-l mw4 black-50 mt5" />
                    

                    <!-- Widget 3: Recent Posts -->
                    <div class="mt5 tc tl-l">
    <h3>近期文章</h3>
    
        <p>
            <a href="/2020/04/01/%E6%B3%9B%E5%9E%8B%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86/">泛型相关知识</a>
        </p>
    
        <p>
            <a href="/2020/03/14/String-s-abc-%E5%92%8C-String-s-new-String-abc-%E7%9A%84%E5%8C%BA%E5%88%AB/">String s=&#34;abc&#34; 和 String s=new String(&#34;abc&#34;)的区别</a>
        </p>
    
        <p>
            <a href="/2020/03/11/%E5%B0%8F%E7%B1%B3java%E5%AE%9E%E4%B9%A0%E9%9D%A2%E7%BB%8F/">小米java实习面经</a>
        </p>
    
        <p>
            <a href="/2020/03/11/Spring%E5%88%9D%E5%A7%8B%E5%8C%96%E7%9A%84%E6%AD%A5%E9%AA%A4/">Spring初始化的步骤</a>
        </p>
    
        <p>
            <a href="/2020/03/11/%E5%85%B3%E4%BA%8E%E4%B8%89%E5%A4%A7IO%E7%9A%84%E7%90%86%E8%A7%A3-AIO-BIO-NIO/">关于三大IO的理解:BIO,NIO,AIO</a>
        </p>
    
</div>
                </div>
            </div>
        </div>
    </div>
</div>


<!-- Footer -->
<div class="bg-1 ph2 ph5-ns pv5">
    <div class="mv8">
        <div class="center tc">
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="https://twitter.com/?lang=en" target="_blank">
                        <i class="fa fa-twitter"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="https://www.facebook.com/" target="_blank">
                        <i class="fa fa-facebook"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="https://dribbble.com/" target="_blank">
                        <i class="fa fa-dribbble"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="https://github.com/ZZZXXXCCCWXY999/" target="_blank">
                        <i class="fa fa-github"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="https://plus.google.com/" target="_blank">
                        <i class="fa fa-google-plus"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="https://www.behance.net/" target="_blank">
                        <i class="fa fa-behance"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="https://500px.com/" target="_blank">
                        <i class="fa fa-500px"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="mailto:zzzxxxcccwxy999@gmail.com" target="_blank">
                        <i class="fa fa-envelope"></i>
                    </a>
                </div>
            
                <div class="dib mh3">
                    <a class="f3 f2-ns white dim" href="/#" target="_blank">
                        <i class="fa fa-rss"></i>
                    </a>
                </div>
            
        </div>
        <div class="f6 f5-ns center tc white pt5 fw3">
            2020 KTG知识技术小组 | ZZZXXXCCCWXY999 | 真是一个巨星
        </div>
        
            <!-- 不蒜子统计 -->
            <div style="text-align:center;color:#FFFFFF">
            <span id="busuanzi_container_site_pv">
                本站总访问量<span id="busuanzi_value_site_pv"></span>次
        </span>
                <span class="post-meta-divider">|</span>
<!--                <span id="busuanzi_container_site_uv" style='display:none'>-->
<!--                本站访客数<span id="busuanzi_value_site_uv"></span>人-->
<!--        </span>-->
            </div>
<!--            <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>-->
            <script async src="/js/busuanzi.pure.mini.js"></script>
        

    </div>
</div>

<!-- After Footer -->
<!-- Disqus Comments -->



</body>

</html>